package com.deke.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deke.entity.CrmBanner;
import com.deke.home.dao.CrmBannerDao;
import com.deke.home.service.CrmBannerService;
import com.deke.home.service.OssService;
import com.deke.home.vo.CrmBannerVo;
import com.deke.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/12/6 19:00
 * @description：
 * @modified By：
 * @version:
 */
@Api(tags = "轮播管理")
@RestController
@RequestMapping("/home/banner")
@SuppressWarnings({"all"})
public class CrmBannerController {

    @Autowired
    private CrmBannerService crmBannerService;

    @Autowired
    private CrmBannerDao crmBannerDao;

    @Autowired
    private OssService ossService;

    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的条数")
    })
    @PostMapping("getAllBanner/{currentPage}/{pageSize}")
    public Result<IPage<CrmBanner>> getAllBanner(@PathVariable(required = true, value = "currentPage") Integer current, @PathVariable(required = true) Integer pageSize, @RequestBody
            CrmBannerVo crmBannerVo) {
        /*String username = WebUtils.getUsername();
        List<AclPermission> permissionValueList = crmBannerDao.findPermissionByUserName(username);
        List<String> permissionValue = new ArrayList<>();
        for (AclPermission aclPermission : permissionValueList) {
            if (ObjectUtil.isNotEmpty(aclPermission.getPermissionValue())){
                permissionValue.add(aclPermission.getPermissionValue());

            }
        }
        System.out.println("=================================");
        System.out.println(permissionValue);*/
        return crmBannerService.queryByConditionPage(current, pageSize, crmBannerVo);

    }


    @ApiOperation(value = "改变状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id"),
            @ApiImplicitParam(name = "isDisable", value = "状态(0或1)")
    })
    @PostMapping("changeStatus/{id}/{isDisable}")
    public Result changeStatus(@PathVariable(required = true) String id, @PathVariable(required = true) String isDisable) {
        return crmBannerService.changeStatus(id, isDisable);
    }


    @ApiOperation(value = "删除操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id"),
    })
    @PostMapping("deleteBanner")
    public Result deleteBanner(@RequestBody Map<String, String> map) {
        return crmBannerService.deleteBanner(map.get("id"));
    }


    @ApiOperation(value = "添加操作")
    @PostMapping("addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner,MultipartFile file) {
        String url = this.uploadMsg(file);
        return crmBannerService.addBanner(crmBanner);
    }


    @ApiOperation(value = "添加照片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件",paramType = "form",dataType = "file",required = true)
    })
    @PostMapping(value = "uploadMsg")
    public String uploadMsg(@RequestParam("file") MultipartFile file){
        String url = ossService.uploadFileAvatar(file);
        return url;
    }
}